<?php

include_once "db.php";

class Chapter
{
    private $chapter_id;
    private $chapter_name;
    private $region_id;
    private $description;

    private $chapterfound;

    //Takes chapter id to generate the chapter object
    public function __construct($id)
    {
        global $mysqli;
        $this->events = $events;
        $this->projects = $projects;
        $id = mysqli_real_escape_string($mysqli, $id);
        $query = "SELECT * FROM Chapters WHERE ChapterID = '" . $id . "'";
        $data = $mysqli->query($query);
        if (!empty($data))
        {
            $row = $data->fetch_array(MYSQLI_ASSOC);
            $this->chapter_id = intval($row["ChapterID"]);
            $this->chapter_name = $row["ChapterName"];
            $this->region_id = $row["RegionID"];
            $this->description = $row["Description"];
            $this->chapterfound = true;
        } else
        {
            $this->chapterfound = false;
        }
    }

    public function getChapterName()
    {
        return $this->chapter_name;
    }

    public function getChapterId()
    {
        return $this->chapter_id;
    }

    public function getRegionId()
    {
        return $this->region_ids;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function chapterFound()
    {
        return $this->chapterfound;
    }

    //Returns the user id of each eboard memeber of this chapter
    public function getEboardIds()
    {
        $idarray = array();
        $query = "SELECT * FROM Users WHERE ChapterId = '" . $this->chapter_id .
            "' AND PermissionsId > 0";
        $data = $mysqli->query($query);
        while ($row = $data->fetch_array())
        {
            array_push($idarray, $row["UserId"]);
        }
        return idarray;
    }

    private function updateValue($value, $dbstring)
    {
        global $mysqli;
        $query = "UPDATE Chapters SET " . $dbstring . "='" . $value .
            "' WHERE ChapterId='" . $this->chapter_id . "'";
        $mysqli->query($query);
    }

    public function setDescription($desc)
    {
        $this->updateValue($desc, "Description");
        $this->description = $desc;
    }
}

?>